// index.js
if ('serviceWorker' in window.navigator) {
  const serviceWorker = navigator.serviceWorker
  serviceWorker.register('./work.js').then(function (reg) {
    const messageChannel = new MessageChannel();
    messageChannel.port1.onmessage = e => {
      console.log(e.data); // this message is from sw.js, to page
    }
    reg.active.postMessage("this message is from page, to sw", [messageChannel.port1]);
  }).catch(function (err) {
    console.log('fail', err)
  });

  serviceWorker.addEventListener('message', function (e) {
    console.log(e.data);
  })
}



